package com.yc.mvc.web;

import com.yc.mvc.bean.User;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserAction {

    @RequestMapping(path = "register",
    produces = MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
    public User register(User user,
                         HttpServletRequest request,
                         HttpServletResponse response,
                         HttpSession session){
        user.getServlets().add(request);
        user.getServlets().add(response);
        user.getServlets().add(session);
        return user;
    }

    @RequestMapping("add")
    public int add(@RequestParam("x") int a,
                   @RequestParam(value="y", required = false, defaultValue = "1") int b){
        return a + b;
    }

    // sub/5/3
    @RequestMapping("sub/{a}/{b}")
    public int sub(@PathVariable(required = true) int a,
                   @PathVariable int b){
        return a - b;
    }

    @RequestMapping("values")
    public Map values(@RequestHeader String Host,
                      @RequestHeader("User-Agent") String userAgent,
                      @RequestHeader String Cookie, // a=1; b=2; c=3
                      @CookieValue String myName,
                      @RequestBody String body){
        Map map = new HashMap();
        map.put("Host",Host);
        map.put("userAgent",userAgent);
        map.put("Cookie",Cookie);
        map.put("myName",myName);
        map.put("body",body);
        return map;
    }

}
